﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AMDev.BNET.ClientAPI
{
    public static class RegionsEnumerationExtensions
    {

        private static BNETRegionHostAttribute GetRegionHostAttribute(BNETRegionsEnumeration value)
        {
            BNETRegionHostAttribute[] hostAttributes = null;
            BNETRegionHostAttribute defaultHostAttribute = null;
            Type enumType = null;
            FieldInfo fieldInfo = null;

            enumType = typeof(BNETRegionsEnumeration);
#if NETFX_CORE
            fieldInfo = enumType.GetTypeInfo().GetDeclaredField(value.ToString());
#else
            fieldInfo = enumType.GetField(value.ToString());
#endif
            hostAttributes = fieldInfo.GetCustomAttributes(typeof(BNETRegionHostAttribute), false) as BNETRegionHostAttribute[];
            if (hostAttributes != null)
                defaultHostAttribute = hostAttributes.FirstOrDefault();

            return defaultHostAttribute;
        }

        public static String GetHost(this BNETRegionsEnumeration value)
        {
            String host = null;
            BNETRegionHostAttribute hostAttribute = null;

            hostAttribute = GetRegionHostAttribute(value);
            if (hostAttribute != null)
                host = hostAttribute.Hostname;
            return host;
        }

        public static String GetRegion(this BNETRegionsEnumeration value)
        {
            String region = null;
            BNETRegionHostAttribute hostAttribute = null;

            hostAttribute = GetRegionHostAttribute(value);
            if (hostAttribute != null)
                region = hostAttribute.Region;
            return region;
        }

        public static String GetRegionFullname(this BNETRegionsEnumeration value)
        {
            String regionFullname = null;
            BNETRegionHostAttribute hostAttribute = null;

            hostAttribute = GetRegionHostAttribute(value);
            if (hostAttribute != null)
                regionFullname = hostAttribute.RegionFullname;
            return regionFullname;
        }

        public static String[] GetRegionSupportedLocales(this BNETRegionsEnumeration value)
        {
            String[] regionLocales = null;
            BNETRegionHostAttribute hostAttribute = null;

            hostAttribute = GetRegionHostAttribute(value);
            if (hostAttribute != null)
                regionLocales = hostAttribute.RegionLocales;
            return regionLocales;
        }
    }
}
